package com.sr.demo.security.jwt;

import cn.hutool.json.JSONObject;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sr.demo.security.nuouser.dto.NuoUserDTO;
import com.sr.demo.security.user.NuoUser;


import java.util.Date;
import java.util.HashMap;

/**
 *
 *
 *@author liushuai
 *@date 2020/7/28 19:58
 */
public class JwtUtil {

    /**
     * 过期时间为一天
     * TODO 正式上线更换为15分钟
     */
    private static final long EXPIRE_TIME = 15*60*1000;

    /**
     * token私钥
     */
    private static final String TOKEN_SECRET = "3.14159261";

    /**
     * 生成签名,15分钟后过期
     * @param
     * @param
     * @return
     */
    public static String sign(NuoUser user, String auths){

        //过期时间
        Date date = new Date(System.currentTimeMillis() + EXPIRE_TIME);
        //私钥及加密算法
        Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
        //设置头信息
        HashMap<String, Object> header = new HashMap<>(2);
        header.put("typ", "JWT");
        header.put("alg", "HS256");
        //附带username和userID生成签名
        return JWT.create().withHeader(header).withClaim("userName",user.getUsername())
                .withClaim("userCode",user.getUserCode()).withClaim("auths",auths).withExpiresAt(date).sign(algorithm);
    }


    public static boolean verity(String token){
        try {
            Algorithm algorithm = Algorithm.HMAC256(TOKEN_SECRET);
            JWTVerifier verifier = JWT.require(algorithm).build();
            DecodedJWT jwt = verifier.verify(token);
            return true;
        } catch (IllegalArgumentException e) {
            return false;
        } catch (JWTVerificationException e) {
            return false;
        }

    }

    /**
     * 通过header头部的token得到userName
     * @return token中包含的用户名
     */
    public static String getUserName(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("userName").asString();
        } catch (JWTDecodeException e) {
            return null;
        }
    }

    /**
     * 通过header头部的token得到userCode
     * @return token中包含的用户名
     */
    public static String getUserCode(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getClaim("userCode").asString();
        } catch (JWTDecodeException e) {
            return null;
        }
    }
    public static JSONObject getAuths(String token){
        if(StringUtils.isEmpty(token)){
            return new JSONObject();
        }
        try {

            DecodedJWT jwt = JWT.decode(token);
            JSONObject json = new JSONObject(jwt.getClaim("auths").asString());
            return json;
        } catch (JWTDecodeException e) {
            return null;
        }
    }




}
